﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace CosView.GUI
{
    public class Form
    {
        private DrawerObject FormBaseBorder = new DrawerObject(0, 0, 0, 0);

        public Form()
        {
            //FormBaseBorder.UID = Guid.NewGuid().ToString();
            Random r = new Random(15542);
            FormBaseBorder.UID = r.Next(int.MinValue, int.MaxValue).ToString();
            FormBaseBorder.BackColor = ConsoleColor.Gray;
            FormBaseBorder.ForeColor = ConsoleColor.Black;
            FormBaseBorder.Changed += new DrawerObject.EmptyDelegate(FormBaseBorder_Changed);
        }

        void FormBaseBorder_Changed()
        {
            this.Invalidate();
        }

        private int _X;
        public int X
        {
            get { return _X; }
            set { _X = value; FormBaseBorder.StartX = value; }
        }

        private int _Y;
        public int Y
        {
            get { return _Y; }
            set { _Y = value; FormBaseBorder.StartY = value; }
        }

        private int _Width;
        public int Width
        {
            get { return _Width; }
            set { _Width = value; FormBaseBorder.EndX = FormBaseBorder.StartX + value; }
        }

        private int _Height;
        public int Height
        {
            get { return _Height; }
            set { _Height = value; FormBaseBorder.EndY = FormBaseBorder.StartY + value; }
        }

        private string _Text;
        public string Text
        {
            get { return _Text; }
            set { _Text = value; }
        }

        public void Invalidate()
        {
            OnPaint();
        }

        public event EventHandler Paint;
        protected virtual void OnPaint()
        {
            if (Paint != null) Paint(this, EventArgs.Empty);
            bool Setup = false;
            for (int i = 0; i < Drawer.Objects.Count; i++)
            {
                DrawerObject dob = Drawer.Objects[i];
                // It's the form
                if (dob.UID == FormBaseBorder.UID)
                {
                    Drawer.Objects[Drawer.Objects.IndexOf(dob)] = FormBaseBorder;
                    Setup = true;
                }
            }
            if (!Setup)
            {
                Drawer.Objects.Add(FormBaseBorder);
                Setup = true;
            }
        }
    }

    public class ConsoleForm
    {
        private string _Text = "";
        private ConsoleColor _BackColor = ConsoleColor.White;
        private ConsoleColor _BorderColor = ConsoleColor.Gray;
        private ConsoleColor _BorderBackColor = ConsoleColor.Gray;
        private ConsoleColor _ForeColor = ConsoleColor.Black;
        private ConsoleColor _TitleBackColor = ConsoleColor.Blue;
        private ConsoleColor _TitleForeColor = ConsoleColor.White;

        public string Text
        {
            get { return _Text; }
            set { _Text = value; if (TextChanged != null) TextChanged(this, EventArgs.Empty); }
        }
        public ConsoleColor BackColor
        {
            get { return _BackColor; }
            set { _BackColor = value; }
        }
        public ConsoleColor ForeColor
        {
            get { return _ForeColor; }
            set { _ForeColor = value; }
        }
        public ConsoleColor BorderColor
        {
            get { return _BorderColor; }
            set { _BorderColor = value; }
        }
        public ConsoleColor BorderBackColor
        {
            get { return _BorderBackColor; }
            set { _BorderBackColor = value; }
        }
        public ConsoleColor TitleBackColor
        {
            get { return _TitleBackColor; }
            set { _TitleBackColor = value; }
        }
        public ConsoleColor TitleForeColor
        {
            get { return _TitleForeColor; }
            set { _TitleForeColor = value; }
        }

        private void DrawLine(int width)
        {
            for (int i = 1; i < width; i++)
            {
                Console.Write('#');
            }
        }
        private void DrawLine(int width, char cont)
        {
            for (int i = 1; i < width; i++)
            {
                Console.Write(cont);
            }
        }

        private string[] _Lines = new string[] { };
        public string[] Lines
        {
            get { return _Lines; }
            set { _Lines = value; }
        }

        private void DrawText(string line, char borderchar)
        {
            Console.BackgroundColor = BorderBackColor;
            Console.ForegroundColor = BorderColor;
            DrawLine(1, borderchar);
            Console.Write(' ');
            Console.BackgroundColor = BackColor;
            Console.ForegroundColor = ForeColor;
            for (int l = 0; l < line.Length - 4; l++)
            {
                Console.Write(line[l]);
            }
            Console.BackgroundColor = BorderBackColor;
            Console.ForegroundColor = BorderColor;
            Console.Write(' ');
            DrawLine(1, borderchar);
            Console.Write(Environment.NewLine);
        }
        private void DrawText(string[] lines, char borderchar)
        {
            for (int i = 0; i < lines.Length; i++)
            {
                Console.BackgroundColor = BorderBackColor;
                Console.ForegroundColor = BorderColor;
                DrawLine(1, borderchar);
                Console.Write(' ');
                Console.BackgroundColor = BackColor;
                Console.ForegroundColor = ForeColor;
                for (int l = 0; l < lines[i].Length - 4; l++)
                {
                    Console.Write(lines[i][l]);
                }
                Console.BackgroundColor = BorderBackColor;
                Console.ForegroundColor = BorderColor;
                Console.Write(' ');
                DrawLine(1, borderchar);
                Console.Write(Environment.NewLine);
            }
        }

        private void DrawTitle(string title, char borderchar)
        {
            string text = "";
            for (int i = 0; i < Console.WindowWidth - 4; i++)
            {
                text += title[i];
            }
            Console.BackgroundColor = BorderBackColor;
            Console.ForegroundColor = BorderColor;
            Console.Write(borderchar);
            Console.Write(' ');
            Console.BackgroundColor = TitleBackColor;
            Console.ForegroundColor = TitleForeColor;
            Console.Write(text);
            Console.BackgroundColor = BorderBackColor;
            Console.ForegroundColor = BorderColor;
            Console.Write(' ');
            Console.Write(borderchar);
            Console.Write(Environment.NewLine);
        }

        public void Paint()
        {
            Console.Clear();
            int linew = Console.WindowWidth;
            int lines = Console.WindowHeight;
            Console.BackgroundColor = BorderBackColor;
            Console.ForegroundColor = BorderColor;
            DrawLine(linew, '#'); // Line01: Border - Top
            Console.Write(Environment.NewLine);
            DrawTitle(Text, '#'); // Line02: Text
            Console.BackgroundColor = BorderBackColor;
            Console.ForegroundColor = BorderColor;
            DrawLine(linew, '#'); // Line03: Border - Title - Bottom
            Console.Write(Environment.NewLine);
            DrawText(Lines, '#'); // Content
            Console.BackgroundColor = BorderBackColor;
            Console.ForegroundColor = BorderColor;
            DrawLine(linew, '#'); // Bottom Border
        }

        public event EventHandler TextChanged;
    }
}
